﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.SatinAlma;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Collections;
using System.Drawing;

namespace ConfExpert.Forms.Ithalat
{
    public partial class IsTakip : XForm
    {
        public IsTakip()
        {
            InitializeComponent();
        }

        private void IsTakip_Load(object sender, EventArgs e)
        {
            cmbDonem.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            groupOrder = new ArrayList(new string[] { "GÜMRÜKTEKİ İŞLEMLER", "FİRMA BEKLEYEN İŞLEMLER", "ÇIKIŞ İHBARI GELEN YOLDAKİ İŞLEMLER", "ÇIKIŞ İHBARI GELMEYEN YOLDAKİ İŞLEMLER", "ÇEKİLEN İŞLEMLER" });

            int Donem = cmbDonem.EditValue.ToInt();
            Notify(Unsped_Util.Rfrsh(Donem));


            gridView1.Init("GetIsTakip", Donem);
            gridView1.MakeRowSelectable();

            GridColumn col = gridView1.Columns["TUR"];
            col.SortMode = DevExpress.XtraGrid.ColumnSortMode.Custom;
            col.GroupFormat.FormatType = DevExpress.Utils.FormatType.Custom;

            //gridView1.GroupFormat = "{0}: [#image]{1} {2}"; //"{0}: [#image]{1} {2}".
            gridView1.CustomColumnSort += gridView1_CustomColumnSort;

            gridView1.BeginSort();
            //gridView1.Columns["TUR"].Group();

            gridView1.SortInfo.ClearAndAddRange(new GridColumnSortInfo[] { 
                new GridColumnSortInfo(gridView1.Columns["TUR"], DevExpress.Data.ColumnSortOrder.Ascending)
            }, 2);
            gridView1.EndSort();
            gridView1.ExpandAllGroups();

            //foreach (string str in groupOrder)
            //{
            //    int rowHandle = -1;
            //    if (str != "ÇEKİLEN İŞLEMLER")
            //        do
            //        {
            //            rowHandle = gridView1.LocateByValue(rowHandle + 1, gridView1.Columns["TUR"], str);
            //            gridView1.MakeRowVisible(rowHandle, false);
            //        } while (rowHandle != DevExpress.XtraGrid.GridControl.InvalidRowHandle);
            //}

            gridView1.FormatAsDateTime("Tarih");
            gridView1.FormatAsNumber(2, "FaturaTutarı", "Miktar", "BrutKg");

            gridView1.BestFitColumns();
            gridView1.RowStyle += gridView1_RowStyle;

            gridView1.OptionsView.GroupFooterShowMode = GroupFooterShowMode.VisibleAlways;
            // Create and setup the first summary item.
            GridGroupSummaryItem item = new GridGroupSummaryItem();
            item.FieldName = "UNSPED_DOSYA_NO";
            item.SummaryType = DevExpress.Data.SummaryItemType.Count;
            gridView1.GroupSummary.Add(item);
        }


        void gridView1_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e)
        {
            if (e.Column.FieldName == "TUR")
            {
                e.Handled = true;
                e.Result = System.Collections.Comparer.Default.Compare(decode(e.Value1), decode(e.Value2));
            }
        }

        static ArrayList groupOrder;
        private int decode(object v)
        {
            return groupOrder.IndexOf(v);
        }

        void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                switch (gridView1.GetRowCellDisplayText(e.RowHandle, "TUR"))
                {
                    case "ÇIKIŞ İHBARI GELEN YOLDAKİ İŞLEMLER": e.Appearance.ForeColor = Color.Blue; break;
                    case "FİRMA BEKLEYEN İŞLEMLER": e.Appearance.ForeColor = Color.FromArgb(255, 0, 0); break;
                    case "ÇIKIŞ İHBARI GELMEYEN YOLDAKİ İŞLEMLER": e.Appearance.ForeColor = Color.FromArgb(0, 128, 0); break;
                }

                switch (gridView1.GetRowCellDisplayText(e.RowHandle, "Hat"))
                {
                    case "KIRMIZI": e.Appearance.ForeColor = Color.FromArgb(255, 0, 0); break;
                }
            }
        }
    }
}
